// 添加请求
import axios from "axios";

axios.defaults.baseURL = "http://127.0.0.1:3000";

// 添加请求拦截器
axios.interceptors.request.use(
  (req) => {
    // console.log(req);
    //向请求头添加token
    let token = localStorage.getItem("news");
    if (token) {
      req.headers.Authorization = "Bearer " + token;
    }
    return req;
  },
  (error) => {
    console.log(Promise);
    return Promise.reject(error);
  }
);

import { Toast } from "vant";

//response响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  // console.log(response)
  if(response.data.message == '用户信息验证失败'){
      Toast.fail('用户信息验证失败')
      // 1.获取到当前页面的url
      console.log(window.location.hash)
      // 2.做为参数传递给login
      // http://localhost:8080/#/login?returnurl=%2Flogin%3Freturnurl%3D
      window.location.href='#/login?returnurl='+window.location.hash.substr(1)
  }
  return response;
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
});

export default axios;
